package pl.edu.agh.anomalyids.lerad.aspect;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;

import pl.edu.agh.anomalyids.lerad.aspect.enums.AspectType;
import pl.edu.agh.anomalyids.lerad.aspect.enums.Match;
import pl.edu.agh.anomalyids.network.Packet;

public abstract class AbstractAspect<T> implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private AspectType type;
	protected HashSet<T> values;

	public AbstractAspect(AspectType type) {
		this.type = type;
		this.values = new HashSet<T>();
	}

	/**
	 * Getter
	 * 
	 * @return aspect type
	 */
	public AspectType getType() {
		return type;
	}

	/**
	 * Checks if passed value matched the contained one.
	 * 
	 * @param value
	 *            value to check
	 * @return true if match found; false otherwise
	 */
	public boolean validate(T value){
		return values.contains(value);
	}

	/**
	 * Checks if aspect from passed packet matched this aspect.
	 * 
	 * @param packet
	 *            packet to check
	 * @return true if match found; false otherwise
	 */
	public abstract Match packetMatchesAspect(Packet packet);

	public abstract String toString();

	/**
	 * Getter.
	 * 
	 * @return main value of this aspect
	 */
	public T getData(){
		for (T value : values){
			return value;
		}
		return null;
	}

	/**
	 * Getter.
	 * 
	 * @return collection of unique values of this aspect
	 */
	public Collection<T> getValues() {
		return values;
	}

	/**
	 * Adds value of this aspect to the collection of unique values.
	 * 
	 * @param value
	 *            value to add
	 */
	public void addValue(T value) {
		values.add(value);
	}

	/**
	 * Removes value of this aspect from the collection of unique values.
	 * 
	 * @param value
	 *            value to remove
	 */
	public void delValue(T value) {
		values.remove(value);
	}
	
	/**
	 * Wipes whole collection of unique values.
	 */
	public void clearValues(){
		values.clear();
	}
}
